<?php
# this file contais core functions for home page

# function will return array of groups for current user
function c_home_get_groups(){
	$current_user = $_SESSION['user_id'];
	return c_get_user_groups($current_user);
}

# function will return list of all groups for given user
function c_get_user_groups($user_id){
	$grpArr = array();
	
	$query = "SELECT g.ID, g.NAME FROM participants p JOIN groups g ON p.GROUP_ID = g.ID WHERE p.USER_ID = $user_id AND p.STATUS = 1";  
	$result = c_db_query($query);
	
	if($result){
		while($row = mysql_fetch_array($result)) 
		{
			$grpArr[] = array('id' => $row[0], 'name' => $row[1]);
		}
	}
	return $grpArr;
}

# function will return array of meetings for current user
function c_home_get_meetings(){
	$current_user = $_SESSION['user_id'];
	return c_get_user_meetings($current_user);
}

# function will return list of all meetings for given user
function c_get_user_meetings($user_id){
	$grpArr = array();
	
	$query = "SELECT g.NAME, m.S_TIME, m.E_TIME, m.PLACE
				FROM def_meeting m 
				JOIN groups g ON m.GROUP_ID = g.ID 
				JOIN participants p ON p.GROUP_ID = g.ID
				WHERE p.USER_ID = $user_id 
					AND p.STATUS = 1
					AND m.S_TIME > NOW()
				ORDER BY m.S_TIME ASC";  
	$result = c_db_query($query);
	
	if($result){
		while($row = mysql_fetch_array($result)) 
		{
			$row_time = c_calc_time($row[1], $row[2]);
			
			$grpArr[] = array('group_name' => $row[0], 'date' => $row_time['date'], 
				'time' => $row_time['time'], 'place' => $row[3], 'duration' => $row_time['duration']);
		}
	}
	
	return $grpArr;
}

#function that will return duration between two times
function c_calc_time($s_date, $e_date){
	$format_date = 'Y-m-d';
	$format_time = 'H:i';
	$s_time = strtotime($s_date);
	$e_time = strtotime($e_date);
	
	$date = date($format_date, $s_time);
	$time = date($format_time, $s_time);
	$duration = ($e_time - $s_time)/3600; // divide to miliseconds in hour
	
	return array(
		'duration' => strval($duration),
		'date' => $date,
		'time' => $time);
}

# function that will return current date of timetable
function c_get_time_curr_date(){
	$time_year = date('Y');
	$time_month = date('m');
	$time_day = date('d');
	
	$dateArr = [
		'year'  => $time_year,
		'month' => $time_month,
		'day'   => $time_day
	];
	
	if(isset($_GET['year']) && isset($_GET['month']) && isset($_GET['day'])){
		$year  = $_GET['year'];
		$month = $_GET['month'];
		$day   = $_GET['day'];
		
		// TODO check correctness of date
		
		if(true){ // TODO insert check
			$dateArr = [
				'year'  => $year,
				'month' => $month,
				'day'   => $day
			];
		}
	}
	return $dateArr;
}

# function that will return next date for timetable
function c_get_time_next_date($dateArr){
	
	$inputDate = $dateArr['year'].'-'.$dateArr['month'].'-'.$dateArr['day'];
	$timestamp = strtotime($inputDate);
	$dateArr['day'] = date('d', strtotime('+1 day', $timestamp));
	$dateArr['month'] = date('m', strtotime('+1 day', $timestamp));
	$dateArr['year'] = date('Y', strtotime('+1 day', $timestamp));
	return $dateArr;
}

# function that will return previous date for timetable
function c_get_time_prev_date($dateArr){
	$inputDate = $dateArr['year'].'-'.$dateArr['month'].'-'.$dateArr['day'];
	$timestamp = strtotime($inputDate);
	$dateArr['day'] = date('d', strtotime('-1 day', $timestamp));
	$dateArr['month'] = date('m', strtotime('-1 day', $timestamp));
	$dateArr['year'] = date('Y', strtotime('-1 day', $timestamp));
	return $dateArr;
}

#function that will return array with possible times for selected day.
function c_get_time_values($dateArr){
	$timeArr = array();
	
	$date = $dateArr['year'].'-'.$dateArr['month'].'-'.$dateArr['day'];
	$user = $_SESSION['user_id'];
	
	$query = "SELECT hour FROM timetable WHERE user_id=$user and date='$date' ORDER BY hour ASC";
	$result = c_db_query($query);
	
	if($result){
		while($row = mysql_fetch_array($result)) 
		{
			$timeArr[] = intval($row[0]);
		}
	}
	
	return $timeArr;
}
?>